/**
 * Created by ritamashin on 2017/11/3.
 */
import {AsyncStorage} from 'react-native'
import {createStore, applyMiddleware,compose} from 'redux'
import thunk from 'redux-thunk'
import {createLogger} from 'redux-logger'
import {autoRehydrate, persistStore as reduxPersistStore} from 'redux-persist'
import {IN_DEBUGGER} from './config'
import reducer from './reducer'

const middlewares = [thunk];

if (IN_DEBUGGER){
    middlewares.push(createLogger({
        duration: true,
        collapsed: true,
    }))
}

const store = createStore(reducer, undefined, compose(
    applyMiddleware(...middlewares),
    autoRehydrate()
))

export function persistStore(store, cbOk, cbFail) {
    reduxPersistStore(
        store,
        {
            storage : AsyncStorage,
            blacklist: []
        },
        (error, state) => {
            if (error){
                cbFail(error)
            }else {
                cbOk(state)
            }
        }
    )
}

export default store